package com.jw.qme.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.jw.qme.domain.dto.GroupDTO;
import com.jw.qme.domain.dto.MessageGroupDTO;
import com.jw.qme.service.GroupMemberService;
import com.jw.qme.service.GroupService;
import com.jw.qme.service.MessageGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: com.jw.qme.controller
 * Datetime: 2025/4/5 下午6:49
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    private final GroupMemberService groupMemberService;

    private final MessageGroupService messageGroupService;

    /**
     * 创建群聊
     * @param groupDto
     * @return
     */
    @PostMapping("/group/create")
    public SaResult createGroup(@RequestBody GroupDTO groupDto){
        return SaResult.data(groupService.createGroup(groupDto));
    }

    /**
     * 根据群号来查询群聊
     * @param groupId
     * @return
     */
    @GetMapping("/group/get/{groupId}")
    public SaResult getGroupByNo(@PathVariable String groupId){
        return SaResult.data(groupService.getGroupByNo(groupId));
    }

    /**
     * 删除对应的群聊 级联跟新对应的还要删除对应的群成员
     * @param groupId
     * @return
     */
    @GetMapping("/group/delete/{groupId}")
    public SaResult deleteGroup(@PathVariable String groupId){
        return SaResult.data(groupService.deleteGroup(groupId));
    }

    /**
     * 获取群成员
     * @param groupId
     * @return
     */
    @GetMapping("/group/members/{groupId}")
    public SaResult getGroupMembers(@PathVariable String groupId){
        return SaResult.data(groupMemberService.getGroupMembers(groupId));
    }

    /**
     * 获取所有群聊
     * @param username
     * @return
     */
    @GetMapping("/gropp/all/{username}")
    public SaResult getAllGroups(@PathVariable String username){
        return SaResult.data(groupService.getAllGroups(username));
    }

    /**
     * 获取群聊历史信息
     * @param messageGroupDTO
     * @return
     */
    @GetMapping("/group/message")
    public SaResult getGroupMessages(@RequestBody MessageGroupDTO messageGroupDTO){
        return SaResult.data(messageGroupService.listByGroupId(messageGroupDTO));
    }

    /**
     * 申请加入群聊
     * @param groupId
     * @return
     */
    @PostMapping("/group/join/{groupId}")
    public SaResult joinGroup(@PathVariable String groupId){
        return SaResult.data(groupService.joinGroup(groupId));
    }


}
